<?php

/**
 * @Author:      yyc
 * @DateTime:    18-7-18 上午11:30
 * @Description: 消息控制器
 * Class OANewsNoticeController
 */
class OANewsNoticeController extends BaseController
{
    /**
     * @var object 验证方法对象
     */
    protected $validator;
    /**
     * @var array 返回数据模板
     */
    protected $response = ['code' => 0, 'msg' => 'ok'];
    /**
     * @var object 用户信息
     */
    protected $user;

    /**
     * @Author:      yyc
     * @DateTime:    18-7-18 上午11:00
     * @Description: 验证用户token
     * OANewsNoticeController constructor.
     */
    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
	header('Access-Control-Allow-Credentials', 'true');
        
        $this->user = $this->verificationOAUserToken(Input::get('token'), $this->response);
    }

    /**
     * @Author:      yyc
     * @DateTime:    18-7-18 上午12:00
     * @Description: 获取消息信息
     * @link: http://api.anhui.com/oa/newsnotice/index?token=cd42ad78766754e9a663e4ed43b64c02
     * @return string
     */
    public function index()
    {
        $this->validator = Validator::make(Input::all(), ['token' => 'required']);
        do {
            //验证token
            if ($this->validator->fails()) {
                $this->response = Lang::get('oa_error.args_error_100000');
                $this->response += ['result' => $this->validator->messages()];
                break;
            }
            //验证用户
            if (!$this->user) {
                break;
            }

            //获取个人消息信息
            $this->response = [
                'code' => 0,
                'msg' => 'ok',
                'result' => OANewsNotice::getNewsNoticeInfo($this->user->id)
            ];
        } while (false);

        //返回数据
        Log::info('info', [
            'system' => 'oa',
            'event' => 'newsnotice/index',
            'input' => Input::all(),
            'output' => $this->response
        ]);

        return json_encode($this->response);
    }

    /**
     * @Author:      yyc
     * @DateTime:    18-7-18 下午15:00
     * @Description: 修改消息状态
     * @link: http://api.anhui.com/oa/newsnotice/read?token=cd42ad78766754e9a663e4ed43b64c02&id=1,2,&status=1
     * @return string
     */
    public function readStatus()
    {
        $this->validator = Validator::make(Input::all(),[
            'token' => 'required',
            'id' => 'required|array',
            //'status' => 'required|numeric|between:1,3',
        ]);
        do {
            //验证token
            if ($this->validator->fails()) {
                $this->response = Lang::get('oa_error.args_error_100000');
                $this->response['result'] = $this->validator->messages();
                break;
            }
            //验证用户信息
            if (!$this->user) {
                break;
            }

            //获取前台数据
            $id = Input::get('id');
            $status = 3;///Input::get('status', 3);

            $statusDesc = [
                1 => '修改为未读',
                2 => '修改为已读',
                3 => '修改为已忽略',
                4 => '修改为已删除'
            ];

            //数据更新
            try {
                OANewsNotice::whereIn('id', $id)->update([
                    'status' => $status, 'updated_by' => $this->user->id
                ]);
            } catch (Exception $e) {
                Log::info('info', [
                    'system' => 'oa',
                    'event' => 'newsnotice/readStatus',
                    'input' => Input::all(),
                    'exception' => $e
                ]);
                $this->response = Lang::get('oa_error.news_notice_readStatus_error_211101');
                break;
            }
            try {
                OAOpLog::writeLog(
                    'newsnotice/readStatus', 2,
                    $statusDesc[$status],
                    json_encode(['消息ID' => $id]),
                    json_encode(['修改后状态' => $status]),
                    $this->user->id
                );
            } catch (Exception $e) {
                Log::info('info', ['system' => 'oa', 'event' => 'newsnotice/readStatus', 'input' => Input::all(), 'exception' => $e]);
                $this->response = Lang::get('oa_error.new_log_write_error_212002');
                break;
            }
        } while (false);
        //系统日志
        Log::info('info', [
            'system' => 'oa',
            'event' => 'newsnotice/readStatus',
            'input' => Input::all(),
            'output' => $this->response,
        ]);
        return json_encode($this->response);
        
    }

}